
package com.pp.admin.main.client.tab;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.pp.admin.common.client.PermissionCheck;
import com.pp.admin.common.client.event.AfterLoginHandler;
import com.pp.admin.common.client.event.AfterLogoutHandler;
import com.pp.admin.common.client.helper.AdminTabHTML;
import com.pp.admin.common.client.helper.Resource;
import com.pp.admin.main.client.Administrator;

public class RootTab extends BaseTab implements AfterLoginHandler, AfterLogoutHandler {
	
	private Administrator	parent		= null;
	
	private HomePage		homePage	= null;
	
	private DomainPage		domainPage	= null;
	
	private UserPage		userPage	= null;
	
	private AdminTab		admin		= null;
	
	/**
	 * @param barHeight
	 * @param barUnit
	 */
	public RootTab( Administrator parent, double barHeight, Unit barUnit ) {

		super( barHeight, barUnit );
		this.parent = parent;
		homePage = new HomePage( this );
		this.add( homePage, new AdminTabHTML( myConstants.homepage(), Resource.HOME_IMG_URL ) );
		this.selectTab(0);
		this.addSelectionHandler( new SelectionHandler<Integer>() {
			
			@Override
			public void onSelection( SelectionEvent<Integer> event ) {
				
				com.pp.admin.main.client.tab.SelectionHandler handler = (com.pp.admin.main.client.tab.SelectionHandler) RootTab.this.getWidget( event.getSelectedItem() );
				handler.refresh();
			}
		} );
	}
	
	public Administrator getAdminParent() {

		return parent;
	}
	
	@Override
	public void afterLogin() {

		if ( parent.checkPermission( PermissionCheck.ADMIN_TAB ) ) {
			admin = new AdminTab( this, 35, Unit.PX );
			this.add( admin, new AdminTabHTML( myConstants.administration(), Resource.ADMIN_IMG_URL ) );
		}
		if ( parent.checkPermission( PermissionCheck.DOMAIN_PAGE ) ) {
			domainPage = new DomainPage( this );
			this.add( domainPage, new AdminTabHTML( myConstants.domains(), Resource.DOMAIN_IMG_URL ) );
		}
		if ( parent.checkPermission( PermissionCheck.USER_PAGE ) ) {
			userPage = new UserPage( this );
			this.add( userPage, new AdminTabHTML( myConstants.users(), Resource.USER_IMG_URL ) );
		}
		// mainPanel.add(new HTML("the other"), "[the other]");
	}
	
	@Override
	public void afterLogout() {

		int totalTabCount = this.getWidgetCount();
		if ( totalTabCount > 1 ) {
			for ( int index = totalTabCount - 1; index > 0; index-- ) {
				this.remove( index );
			}
		}
		afterLogin();
	}
	
	public boolean checkPermission( String permission ) {

		return parent.checkPermission( permission );
	}
}
